package com.happystudy.tools;

import org.thymeleaf.context.IExpressionContext;
import org.thymeleaf.context.IWebContext;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.linkbuilder.StandardLinkBuilder;

import java.util.Map;

/**
 * @author LYJ
 */
public class BaseUrlLinkBuilder  extends StandardLinkBuilder {

    private String baseUrl;

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    @Override
    protected String computeContextPath(IExpressionContext context, String base, Map<String, Object> parameters) {
        if (!(context instanceof IWebContext)) {
            throw new TemplateProcessingException(
                    "Link base \"" + base + "\" cannot be context relative (/...) unless the context " +
                            "used for executing the engine implements the " + IWebContext.class.getName() + " interface");
        }

        if(baseUrl==null){
            throw new TemplateProcessingException("baseUrl 不能为空");
        }

        return baseUrl;
    }

}
